package cn.bulgat.spring.web.model.enums;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import lombok.Getter;
import org.apache.commons.lang3.ObjectUtils;

/**
 * 用户角色枚举
 *
 */
@Getter
public enum UserRole {

    USER(1,"用户", "user"),
    ADMIN(2,"管理员", "admin"),
    BAN(3,"被封号", "ban");

    private final Integer code;

    private final String text;

    private final String value;

    UserRole(int code, String text, String value) {
        this.code = code;
        this.text = text;
        this.value = value;
    }

    /**
     * 获取值列表
     *
     * @return
     */
    public static List<String> getValues() {
        return Arrays.stream(values()).map(item -> item.value).collect(Collectors.toList());
    }
    /**
     * 获取值列表
     *
     * @return
     */
    public static List<Integer> getCodes() {
        return Arrays.stream(values()).map(item -> item.code).collect(Collectors.toList());
    }
    /**
     * 根据 value 获取枚举
     *
     * @param value
     * @return
     */
    public static UserRole getByValue(String value) {
        if (ObjectUtils.isEmpty(value)) {
            return null;
        }
        for (UserRole anEnum : UserRole.values()) {
            if (anEnum.value.equals(value)) {
                return anEnum;
            }
        }
        return null;
    }
    /**
     * 根据 code 获取枚举
     *
     * @param code
     * @return
     */
    public static UserRole getByCode(Integer code) {
        if (ObjectUtils.isEmpty(code)) {
            return null;
        }
        for (UserRole anEnum : UserRole.values()) {
            if (anEnum.code.equals(code)) {
                return anEnum;
            }
        }
        return null;
    }
}
